package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("RulecolumnDAO")
@Transactional
public class RulecolumnDAOImpl extends AbstractJpaDao<Rulecolumn> implements RulecolumnDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Rulecolumn.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public RulecolumnDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Rulecolumn findRulecolumnById(Integer id) throws DataAccessException {

		return findRulecolumnById(id, -1, -1);
	}

	@Transactional
	public Rulecolumn findRulecolumnById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulecolumnById", startResult, maxRows, id);
			return (Rulecolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnByName(String name) throws DataAccessException {

		return findRulecolumnByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRulecolumnByName", startResult, maxRows, name);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnByNameContaining(String name) throws DataAccessException {

		return findRulecolumnByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulecolumnByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnByExcelcolumn(Integer excelcolumn) throws DataAccessException {

		return findRulecolumnByExcelcolumn(excelcolumn, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByExcelcolumn(Integer excelcolumn, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulecolumnByExcelcolumn", startResult, maxRows, excelcolumn);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnByExcelcolumnalphabet(String excelcolumnalphabet) throws DataAccessException {

		return findRulecolumnByExcelcolumnalphabet(excelcolumnalphabet, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByExcelcolumnalphabet(String excelcolumnalphabet, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulecolumnByExcelcolumnalphabet", startResult, maxRows,
				excelcolumnalphabet);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnByExcelcolumnalphabetContaining(String excelcolumnalphabet)
			throws DataAccessException {

		return findRulecolumnByExcelcolumnalphabetContaining(excelcolumnalphabet, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByExcelcolumnalphabetContaining(String excelcolumnalphabet, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRulecolumnByExcelcolumnalphabetContaining", startResult, maxRows,
				excelcolumnalphabet);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnByPartion(Integer partion) throws DataAccessException {

		return findRulecolumnByPartion(partion, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByPartion(Integer partion, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulecolumnByPartion", startResult, maxRows, partion);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnByDescription(String description) throws DataAccessException {

		return findRulecolumnByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulecolumnByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnByDescriptionContaining(String description) throws DataAccessException {

		return findRulecolumnByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulecolumnByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@Transactional
	public Rulecolumn findRulecolumnByPrimaryKey(Integer id) throws DataAccessException {
		return findRulecolumnByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Rulecolumn findRulecolumnByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulecolumnByPrimaryKey", startResult, maxRows, id);
			return (Rulecolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulecolumnsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rulecolumn> findAllRulecolumns() throws DataAccessException {

		return findAllRulecolumns(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findAllRulecolumns(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllRulecolumns", startResult, maxRows);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Rulecolumn> findRulecolumnsByRulefield(Integer rulefieldId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllRulecolumnsByRulefield", start, max, rulefieldId);
		return new LinkedHashSet<Rulecolumn>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulecolumn> findRulecolumnByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Rulecolumn>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Rulecolumn> findRulecolumnsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Rulecolumn.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Rulecolumn> rulecolumns = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return rulecolumns;
	}

	@Transactional
	public Rulecolumn findRulecolumnByPhysicalPrimaryKey(Integer rulefieldId, Integer excelcolumn, Integer partion)
			throws DataAccessException {
		return findRulecolumnByPhysicalPrimaryKey(rulefieldId, excelcolumn, partion, -1, -1);
	}

	@Transactional
	public Rulecolumn findRulecolumnByPhysicalPrimaryKey(Integer rulefieldId, Integer excelcolumn, Integer partion,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulecolumnByPhyscialPrimaryKey", startResult, maxRows, rulefieldId,
					excelcolumn, partion);
			return (Rulecolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Rulecolumn entity) {
		return true;
	}
}
